﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fcom.VNInsurance.Web.CultureHelper;
namespace Fcom.VNInsurance.Web.Controllers
{
    public class LanguageController : Controller
    {
        //
        // GET: /Language/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult SetLanguage(string lang,string returnUrl)
        {
            lang = Fcom.VNInsurance.Web.CultureHelper.CultureHelper.GetValidCulture(lang);

            lang = CultureHelper.CultureHelper.GetValidCulture(lang.ToString());
            HttpCookie cultureCookie;
            if (Request.Cookies["_culture"] == null)
            {
                cultureCookie = new HttpCookie("_culture");
                cultureCookie["lang"] = CultureHelper.CultureHelper.GetValidCulture(lang.ToString());

            }
            else
            {
                cultureCookie = Request.Cookies["_culture"];
                cultureCookie["lang"] = CultureHelper.CultureHelper.GetValidCulture(lang.ToString());
            }
            Response.Cookies.Add(cultureCookie);
            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        [HttpPost]
        public ActionResult AjaxLang(string lang)
        {
            lang = CultureHelper.CultureHelper.GetValidCulture(lang.ToString());
            HttpCookie cultureCookie;
            if (Request.Cookies["_culture"] == null)
            {
                cultureCookie = new HttpCookie("_culture");
                cultureCookie["lang"] = CultureHelper.CultureHelper.GetValidCulture(lang.ToString());

            }
            else
            {
                cultureCookie = Request.Cookies["_culture"];
                cultureCookie["lang"] = CultureHelper.CultureHelper.GetValidCulture(lang.ToString());
            }
            Response.Cookies.Add(cultureCookie);
            return Json(new {sucess=true});
        }
    }
}
